<template>
    <div>
        <el-dialog width="30%" title="添加客户档案" :model-value="show" @close="emit('hide')">
            <el-form :model="form" ref="formRef" label-width="100px" class="demo-ruleForm" :rules="rules">
                <el-form-item label="客户姓名" prop="name">
                    <el-input v-model="form.name" autocomplete="off" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="form.phone" autocomplete="off" placeholder="请输入电话" />
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="选择性别">
                        <el-option label="男" value="男" />
                        <el-option label="女" value="女" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="addFn">
                        确定
                    </el-button>
                    <el-button @click="setFn">取消</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang='ts'>
import { ElMessage, FormInstance } from 'element-plus';
import { reactive, ref } from 'vue';
import { AddCustomerUserApi } from './api';

const formRef = ref<FormInstance>();
const props = defineProps({
    show: {
        type: Boolean,
        default: false,
    },
    id: {
        type: Number,
        default: 0,
    },
})

const emit = defineEmits(["hide"]);
const form = reactive<ICustomerModel>({
    id: 0,
    name: "",
    phone: "",
    sex: ""
});

const rules = reactive({
    name: [{ required: true, trigger: "blur", message: "请输入名称" }],
    phone: [{ required: true, trigger: "blur", message: "请输入电话" }],
})

const addFn = () => {
    AddCustomerUserApi(form).then((res) => {
        if (res.code === 200) {
            ElMessage.success("添加成功");
            formRef.value?.resetFields();

            emit("hide");
        }

    })
}

const setFn = () => {
    emit("hide");
}

</script>

<style lang="less" scoped>
.el-button--text {
    margin-right: 15px;
}

:deep(.el-dialog__title) {
    float: left;
}

.el-input {
    width: 100%;
    // width: 350px;
}

:deep(.el-form-item__label) {
    font-weight: 600;
}

.el-button {
    width: 80px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>